﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class UserControl_PositionII : System.Web.UI.UserControl
{
    List<int> _positionIDList = new List<int>();
    public List<int> Select
    {
        get
        {
            if (ViewState["position"] != null)
            {
                return (List<int>)ViewState["position"];
            }
            else
            {
                return null;
            }
        }
        set
        {
            _positionIDList = value;
            if (ViewState["position"] != null)
            {
                ViewState["position"] = _positionIDList;
            }
            else
            {
                ViewState.Add("position", _positionIDList);
            }
            if (_positionIDList != null || _positionIDList.Count > 0)
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    DropDownList1.DataBind();
                    DropDownList1.SelectedValue = _adc.Position.FirstOrDefault(p => p.ID == _positionIDList[0]).FatherID.ToString();
                    CheckBoxList1.DataBind();

                    foreach (ListItem _item in CheckBoxList1.Items)
                    {
                        foreach (var _i in _positionIDList)
                        {

                            if (_item.Value == _i.ToString())
                            {
                                _item.Selected = true;
                                lbl_Select.Text += _adc.Position.FirstOrDefault(i => i.ID == _i).Name + " ";
                            }
                        }
                    }
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        lbl_Select.Text = "";
        using (var _adc = DBHelper.GetDataContext())
        {
            foreach (ListItem _item in CheckBoxList1.Items)
            {
                if (_item.Selected == true)
                {
                    lbl_Select.Text += _item.Text + "&nbsp;&nbsp;&nbsp;";
                    _positionIDList.Add(Convert.ToInt32(_item.Value));
                }
            }
        }
        if (ViewState["position"] == null)
        {
            ViewState.Add("position", _positionIDList);
        }
        else
        {
            ViewState["position"] = _positionIDList;
        }
    }
}
